package org.mineground.commands.player.admin;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;

import org.mineground.Main;
import org.mineground.PermissionNodes;
import org.mineground.Utilities;

public class Weather implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) throws CommandException {
	Player player = (Player) sender;

	if (!player.hasPermission(PermissionNodes.PERMISSION_WEATHER_COMMAND)) {
            return true;
        }

	if (args.length == 0) {
	    player.sendMessage(ChatColor.RED + "* Usage: '/weather <sun/rain>'");
	    return true;
	}

	if (args[0] == null) {
	    player.sendMessage(ChatColor.RED + "* Usage: '/weather <sun/rain>'");
	    return true;
	}

	if (args[0].equalsIgnoreCase("sun")) {
	    player.getWorld().setStorm(false);
	    player.getWorld().setWeatherDuration(1000000);
	    Utilities.sendMessageToAll(ChatColor.GRAY + "* " + player.getDisplayName() + " has changed the weather to sun.");
	    Main.getInstance().getIRCHandler().sendMessage(Main.getInstance().getConfigHandler().ircChannel, Colors.DARK_GREEN + "* " + Utilities.fixName(player) + Colors.DARK_GREEN + " has changed the weather to sun.");
	}

	else if (args[0].equals("rain")) {
	    player.getWorld().setStorm(true);
	    player.getWorld().setWeatherDuration(1000000);
	    Utilities.sendMessageToAll(ChatColor.GRAY + "* " + player.getDisplayName() + " has changed the weather to rain.");
	    Main.getInstance().getIRCHandler().sendMessage(Main.getInstance().getConfigHandler().ircChannel, Colors.DARK_GREEN + "* " + Utilities.fixName(player) + Colors.DARK_GREEN + " has changed the weather to rain.");
	}

	else {
	    player.sendMessage(ChatColor.RED + "* Usage: '/time <day/night>'");
	    return true;
	}
        
	return true;
    }
}